package com.cskaoyan.market.filter;

import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA 2021.
 * User: Lazarus_Liu
 * Date: 2023/8/7
 * Time: 17:20
 * Description:
 */

public class CrosFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {

        // 跨域设置
        //表示从哪个主机过来的请求允许访问后端服务器
        response.setHeader("Access-Control-Allow-Origin", "http://localhost:9527");
        //发送请求时，所允许的请求方法
        response.setHeader("Access-Control-Allow-Methods","POST,GET,OPTIONS,PUT,DELETE");
        //发送请求时允许携带的请求头信息
        response.setHeader("Access-Control-Allow-Headers","x-requested-with,Authorization,Content-Type,X-CskaoyanMarket-Admin-Token,X-CskaoyanMarket-Token");
        //是否允许携带cookie的凭证
        response.setHeader("Access-Control-Allow-Credentials","true");

        // 通过请求
        filterChain.doFilter(request, response);
    }
}
